from django.db import models
from django.contrib.auth.models import AbstractUser
from pip._internal.utils._jaraco_text import _

from py_web.common.db.DelBaseModel import DelBaseModel

# Create your models here.
"""
动态路由 基于RBAC
1.用户表

2.用户和角色关联表

3.菜单表

4.角色信息表

5.角色和菜单关联表

关联关系：
不使用外键
    1.用户表
        
    2.用户和角色关联表
        用户表（userid） 角色表（roleid）
    
    3.菜单表
    
    4.角色信息表
        角色表（roleId） 菜单表（menuId）
"""


# 用户表 继承django自带的用户表字段
class User(AbstractUser):
    id = models.AutoField(db_column="id", verbose_name="user_id", primary_key=True)
    nickname = models.CharField(max_length=20, verbose_name="用户昵称", null=True, blank=True)
    phone = models.CharField(max_length=11, verbose_name="电话号码", null=True, blank=True)
    avatar = models.ImageField(verbose_name="头像url", null=True, blank=True)
    email = models.EmailField(_("email address"), blank=True, null=True)
    status = models.CharField(
        default='0',
        help_text=_(
            "Designates whether this user should be treated as active. "
            "Unselect this instead of deleting accounts."
        ),
        max_length=1,
        verbose_name="status状态（0正常，1异常）",
        null=True, blank=True
    )
    remark = models.CharField(db_column='remark', max_length=64, verbose_name="备注", blank=True, null=True)
    crate_time = models.DateTimeField(null=True, blank=True, auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(null=True, blank=True, auto_now_add=True,
                                       verbose_name="更新时间")

    class Meta:
        db_table = "user"
        verbose_name = "用户表"


class UserRole(DelBaseModel):
    userRoleId = models.AutoField(db_column="user_role_id", verbose_name="user_role_id", primary_key=True)
    userId = models.IntegerField(db_column='user_id', verbose_name="用户id")
    roleId = models.IntegerField(db_column='role_id', verbose_name="角色id")

    class Meta:
        db_table = "user_role"
        verbose_name = "用户和角色关联表"


class Menu(DelBaseModel):
    menuId = models.AutoField(db_column='menu_id', verbose_name="id", primary_key=True)
    menuName = models.CharField(db_column='menu_name', max_length=64, verbose_name="菜单名称", blank=True, null=True)
    parentId = models.IntegerField(db_column='parent_id', blank=True, null=True, verbose_name="父菜单ID")
    orderNum = models.IntegerField(db_column='order_num', verbose_name="显示顺序", blank=True, null=True)
    name = models.CharField(max_length=64, verbose_name="路由名", blank=True, null=True)
    path = models.CharField(max_length=64, verbose_name="路由地址", blank=True, null=True)
    component = models.CharField(max_length=64, verbose_name="组件路径", blank=True, null=True)
    isFrame = models.CharField(db_column='is_frame', max_length=1, default=True, verbose_name="是否为外链（0是 1否）",
                               blank=True, null=True)
    menuType = models.CharField(max_length=1, verbose_name="菜单类型（M目录 C菜单 F按钮）", db_column='menu_type')
    visible = models.CharField(max_length=1, default='0', verbose_name="菜单状态（0显示 1隐藏）")
    status = models.CharField(max_length=1, default='0', verbose_name="菜单状态（0正常 1停用）")
    perms = models.CharField(verbose_name="权限标识", max_length=64, blank=True, null=True)
    icon = models.CharField(max_length=64, verbose_name="菜单图标", blank=True, null=True)
    remark = models.CharField(max_length=64, verbose_name="备注", blank=True, null=True)

    class Meta:
        db_table = "menu"
        verbose_name = "菜单权限表"


class Role(DelBaseModel):
    roleId = models.AutoField(db_column='role_id', verbose_name="id", primary_key=True)
    roleName = models.CharField(db_column='role_name', max_length=20, verbose_name="角色名称", blank=True, null=True)
    roleKey = models.CharField(db_column='role_key', max_length=20, verbose_name="角色权限字符串", blank=True,
                               null=True)
    roleSort = models.IntegerField(db_column='role_sort', verbose_name="显示顺序", blank=True, null=True)
    status = models.CharField(max_length=1, default=0, verbose_name="角色状态（0正常 1停用）", blank=True, null=True)
    remark = models.CharField(max_length=64, verbose_name="备注", blank=True, null=True)

    class Meta:
        db_table = "role"
        verbose_name = "角色信息表"




class RoleMenu(DelBaseModel):
    roleMenuId = models.AutoField(db_column="role_menu_id", verbose_name="role_menu_id", primary_key=True)
    roleId = models.IntegerField(db_column="role_id", verbose_name="角色ID")
    menuId = models.IntegerField(db_column='menu_id', verbose_name="菜单ID")

    class Meta:
        db_table = "role_menu"
        verbose_name = "角色和菜单关联表"
